import Vue from 'vue'
import Router from 'vue-router'
import routerConfig from './routerConfig'
import {isArray} from '../utils/assert.js';
Vue.use(Router);

/**
 * 根据routerConfig数组生成路由
 * @param {Object|Array} config
 * @param {Array} routers
 * @returns {array}
 */
const generateRouter = function (config=routerConfig, routers=[]) {
  const keys = Object.keys(config);
  for(let i=0,length = keys.length;i<length;i++){
    if(isArray(keys[i].children)){
      generateRouter(keys[i].children);
    }else{
      let temp = config[keys[i]];
      for(let ii=0,length = config[keys[ii]].length;ii<length;ii++){
        routers.push(temp[ii]);
      }
    }
  }
  return routers;
};

// console.log(generateRouter());

export default new Router({
  routes: generateRouter()
})
